package cms.mager.dao;

import cms.common.Constant;
import cms.common.PageData;
import cms.dao.BaseDao;
import cms.mager.domain.Customer;

import java.util.List;
import java.util.Map;

/**
 * 功能:
 * Author: yuhui
 * Date: 2021/4/9 20:44
 */
public class CustomerDao extends BaseDao {

    /*1、分页查询*/
    public PageData<Customer> pageCustomer(PageData pd, Map param, Object[] pageArr){
        String sqlpage = "select c.id, c.name, c.phone, c.sex, c.company, c.deleted, c.user_id, u.username, c.create_time, c.update_time, c.delete_time from customer c left join user u on c.user_id = u.id where 1=1";
        String[] cou = Constant.CUSTOMER_COLUMN;
        for(int i=0;i<cou.length;i++){
            if(param.get(cou[i]) != null && !"".equals(param.get(cou[i]))){
                if(param.get(cou[i]) instanceof String){
                    sqlpage  = sqlpage+" and "+cou[i]+" like '%"+param.get(cou[i])+"%'";
                }else if(param.get(cou[i]) instanceof Integer){
                    sqlpage = sqlpage+" and c."+cou[i]+"="+param.get(cou[i]);
                }
            }
        }
        String sql = "select count(1) from customer";
        sqlpage = sqlpage + " limit ?,?";
        return findPage(sql,sqlpage,Customer.class,pageArr,pd);
    }

    /*2、添加客户*/
    public boolean addCustomer(Object[] param){
        String sql = "insert into customer(name,phone,sex,company,user_id) values(?,?,?,?,?)";
        return executeUpdate(sql,param);
    }

    /*3、更新客户信息*//*[就更手机号吧]*/
    public boolean updateInfo(Map<String,String> param,String[] pname){
        int customerByPhone = 0;
        String phone = param.get("phone");
        if(phone != null){
            customerByPhone = findCustomerByPhone(param.get("phone"));
        }
        if(customerByPhone == 0){
            Object[] obj = new Object[pname.length+1];
            int y = 0;
            String sql = "update customer set ";
            for(String name : pname){
                sql = sql + name + " = ?,";
                obj[y++] = param.get(name);
            }
            obj[obj.length-1] = param.get("id");
            sql = sql.substring(0,sql.length()-1);
            sql = sql + " where id=?";
            return executeUpdate(sql,obj);
        }else{
            return false;
        }
    }

    /*4、删除客户信息*/
    public boolean delCustormer(int id){
        String sql = "update customer set deleted=2 where id = ?";
        Object[] arr = {id};
        return executeUpdate(sql,arr);
    }

    /*5、根据手机号查询客户id*/
    public int findCustomerByPhone(String phone){
        int i = 0;
        String sql = "select id from customer where phone = ?";
        Object[] arr = {phone};
        List<Customer> one = findOne(sql, Customer.class, arr);
        if(one.size() == 1){
            i =  one.get(0).getId();
        }else if(one.size() == 0){
            i =  0;
        }else if(one.size() > 1 || one.size() < 0){
            i =  -1;
        }
        return i;
    }

    /*6.给多个客户分配业务员*/
    public boolean updateUser(String[] cuIds,String userId){
        String sql = "update customer set user_id = ? where id in(";
        for(String id:cuIds){
            sql = sql + id+",";
        }
        sql = sql+"-1)";
        String[] uid = {userId};
        return executeUpdate(sql,uid);
    }
}
